home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4SDBLD.CPP < prev    next >
C/C++ Source or Header  |  1995-09-05  |  7KB  |  249 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4sdbld.cpp
  5. //   Title:    9-Digit ZIP Code Directory -- on CD-ROM
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains the program entry point for
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <z4.h>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //    Stack size
  45. //----------------------------------------------------------------------------
  46. #if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS)
  47. unsigned _stklen = 0x4000;
  48. #endif
  49.  
  50.  
  51. //----------------------------------------------------------------------------
  52. //    Globals
  53. //----------------------------------------------------------------------------
  54.  
  55. //
  56. // A useless string which is simply embedded in the executable.
  57. //
  58. PSZ __pszCredits__ = "Written by John M Weeder, 1993";
  59.  
  60.  
  61. //
  62. //    This should contain a program description which will be displayed as
  63. //    part of the program's help text.
  64. //
  65. static PCSZ pcszDescription =
  66.     "This program generates the state detail information for the CS file.";
  67.  
  68. //
  69. //    Program command line options
  70. //
  71. static DATACFG dcfg;
  72. static CHAR szConfig[MAX_PATH] = "z4sd";
  73. static BS_CMDOPT acmdopt[] =
  74.     {
  75.     { "config",     (PVOID)szConfig,        CMDOPT_FILESPEC(80), "Configuration file name."},
  76.     BS_CMDOPT_NULL,
  77.     };
  78.  
  79. //
  80. //    Other globals
  81. //
  82. typedef int (*PFNQSORT)(const void *, const void *);
  83.  
  84. #define MAX_COUNTIES        (300)
  85. static CHAR aszCounty[MAX_COUNTIES][MAX_COUNTY+1];
  86. static SIZET cCounties;
  87. static SIZET cCities;
  88. static LONG alZ5Count[Z4_ST_MAX];
  89.  
  90.  
  91. //----------------------------------------------------------------------------
  92. //    Prototypes
  93. //----------------------------------------------------------------------------
  94. static BOOL FN_L StateDetail(Z4_STATE);
  95. static BOOL FN_L StateWrite(Z4_STATE);
  96.  
  97.  
  98.  
  99. //----------------------------------------------------------------------------
  100. //   Description:    Create the detail information for a single state.
  101. //                          Detail information consists of the number of unique cities,
  102. //                        ZIP codes and counties. A list of counties follows.
  103. //    Parameters:    state        State to find detail for.
  104. //       Returns:    TRUE if successful.
  105. //----------------------------------------------------------------------------
  106. static BOOL FN_L StateDetail(Z4_STATE state)
  107. {
  108.  
  109.     Z4_CS_FILE cs_file;
  110.     Z4_CS cs;
  111.  
  112.     if (!cs_file.Open())                        // Open city/state file
  113.         return FALSE;
  114.     if (!Z4Z5CountRead(alZ5Count, sizeof(alZ5Count)))
  115.         return FALSE;
  116.     Output("  %s", Z4_ST_FILE::Abbreviation(state));
  117.     cCounties = 0;
  118.     cCities = 0;
  119.     if (cs_file.First(cs, state))
  120.         {
  121.         do
  122.             {
  123.             if (cs.state != state)
  124.                 break;
  125.  
  126.             Assert(cCities < 0xFFFF);
  127.             cCities++;
  128.             for (SIZET i = 0; i < cCounties; ++i)
  129.                 if (strcmp(aszCounty[i], cs.szCounty) == 0)
  130.                     break;
  131.  
  132.             if (i >= cCounties && cs.szCounty[0])
  133.                 {
  134.                 Assert(cCounties < MAX_COUNTIES);
  135.                 strcpy(aszCounty[cCounties], cs.szCounty);
  136.                 cCounties++;
  137.                 }
  138.             }
  139.         while (cs_file.Next(cs));
  140.         }
  141.     if (!cs_file.IsError() && cCities)
  142.         {
  143.         Output("\n");
  144.         return StateWrite(state);            // Write information
  145.         }
  146.     else
  147.         Output("...not found");
  148.  
  149.     Output("\n");
  150.     return !cs_file.IsError();
  151. }
  152.  
  153.  
  154. //----------------------------------------------------------------------------
  155. //   Description:    Create the detail information for a single state.
  156. //                          Detail information consists of the number of unique cities,
  157. //                        ZIP codes and counties. A list of counties follows.
  158. //    Parameters:    state        State to find detail for.
  159. //       Returns:    TRUE if successful.
  160. //----------------------------------------------------------------------------
  161. static BOOL FN_L StateWrite(Z4_STATE state)
  162. {
  163. static PCSZ pcszName = "DETAIL.$$$";
  164.  
  165.     Z4_SD sd;
  166.     BYTE bVal = 0;
  167.  
  168.     memset(&sd, 0, sizeof(sd));
  169.     qsort(aszCounty, cCounties, sizeof(aszCounty[0]), (PFNQSORT)strcmp);
  170.     sd.usCities = (USHORT)cCities;
  171.     sd.usZips = (USHORT)alZ5Count[state];
  172.     sd.usCounties = (USHORT)cCounties;
  173.  
  174.     for (SIZET i = 0; i < cCounties; ++i)
  175.         sd.cbCounties += strlen(aszCounty[i]) + 1;
  176.  
  177.     sd.cbCounties++;                        // Add for final null terminator
  178.  
  179.     FLAG16 fs = FL_CREATE|FL_TRUNCATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  180.     HF hf;
  181.  
  182.     if (!FileOpen(&hf, pcszName, fs, NULL))
  183.         return FALSE;
  184.  
  185.     if (!FileWrite(hf, &sd, sizeof(sd), 0))
  186.         {
  187.         FileClose(hf);
  188.         return FALSE;
  189.         }
  190.     for (i = 0; i < cCounties; ++i)
  191.        if (!FileWrite(hf, aszCounty[i], strlen(aszCounty[i]) + 1, -1))
  192.            {
  193.            FileClose(hf);
  194.            return FALSE;
  195.            }
  196.     if (!FileWrite(hf, &bVal, sizeof(bVal), 0))
  197.         {
  198.         FileClose(hf);
  199.         return FALSE;
  200.         }
  201.     FileClose(hf);
  202.     PCSZ pszSt = Z4_ST_FILE::Abbreviation(state);
  203.     BOOL fResult = DioAppendFile(NULL, pszSt, DFT_RANDOM, 0, pcszName);
  204.     FnameDelete(pcszName);
  205.     return fResult;
  206. }
  207.  
  208.  
  209. //----------------------------------------------------------------------------
  210. //   Description:    main() - Program entry point
  211. //    Parameters:    Standard C parameters
  212. //       Returns:    DOS return code.
  213. //----------------------------------------------------------------------------
  214. int main(int argc, char **argv)
  215. {
  216. static BS_CFG cfg = CFG_DFT;
  217.     SHORT retval = 99;
  218.  
  219.     //
  220.     //    Initialize base library
  221.     //
  222.     BaseLibraryInitialize(argc, argv, &cfg);
  223.     BaseTitle("$Revision:  93.1  $", __DATE__, __TIME__, "ZIP+4 State Detail File Build");
  224.     if (!BaseTitleHelp(acmdopt, pcszDescription))
  225.         return 99;
  226.  
  227.     DioSetDataPath(getenv("DATA"));
  228.     if (!DataConfigRead(szConfig, &dcfg))
  229.         {
  230.         Output("Failed reading configuration file.\n");
  231.         goto ERROR_EXIT;
  232.         }                                            // Open for writing
  233.     if (!Z4_INQ::Start(Z4_INQ_CX|Z4_INQ_CS|Z4_INQ_ST|Z4_INQ_WRITE))
  234.         goto ERROR_EXIT;
  235.     Z4_STATE state;
  236.     for (state = Z4_ST_FIRST; state <= Z4_ST_LAST; ++(int)Z4_STATE state)
  237.         if (!StateDetail(state))
  238.             goto ERROR_EXIT;
  239.  
  240.     retval = 0;
  241. ERROR_EXIT:
  242.     Z4_INQ::Terminate();
  243.     DioCloseAll();
  244.     return retval;
  245. }
  246. //----------------------------------------------------------------------------
  247. //------------------------------- End of File --------------------------------
  248. //----------------------------------------------------------------------------
  249.